package site.zhouinfo.xml;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * java对象与XML文件相互转换
 *
 * @author zhou
 * @create 2017-08-15 16:47
 * @email zhouinfo@qq.com
 * @blog blog.zhouinfo.site
 */
public class XMLUtil {

    private final static Logger logger = LoggerFactory.getLogger(XMLUtil.class);

    public static String convertToXml(Object o) {
        StringWriter sw = new StringWriter();
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(o, sw);
        } catch (JAXBException e) {
            logger.debug("java对象转换XML出错!");
            e.printStackTrace();
        }
        return sw.toString();
    }

    public static void convertToXml(Object o, String path) {
        try {
            FileWriter fw = new FileWriter(path);
            JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(o, fw);
        } catch (IOException e) {
            logger.debug("读取文件出错: " + path);
            e.printStackTrace();
        } catch (JAXBException e) {
            logger.debug("java对象转换XML出错!");
            e.printStackTrace();
        }
    }

    public static <T> T convertXmlStrToObject(Class<T> clazz, String xmlStr) {
        Object o = null;
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            StringReader stringReader = new StringReader(xmlStr);
            o = unmarshaller.unmarshal(stringReader);
        } catch (JAXBException e) {
            logger.debug("xml字符串转换java对象出错!");
            e.printStackTrace();
        }

        return (T) o;
    }

    public static <T> T convertXmlFileToObject(Class<T> clazz, String path) {
        Object o = null;
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            FileReader fileReader = new FileReader(path);
            o = unmarshaller.unmarshal(fileReader);
        } catch (JAXBException e) {
            logger.debug("xml字符串转换java对象出错!");
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            logger.debug("文件读取出错: " + path);
            e.printStackTrace();
        }
        return (T) o;
    }
}
